package org.seckill.conf;

import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@MapperScan(basePackages = {"org.seckill.dao"})
public class MybatisPlusConfig {
   /*
    * 分页插件，自动识别数据库类型
    * 多租户，请参考官网【插件扩展】
    */
   @Bean
   public PaginationInterceptor paginationInterceptor() {
      return new PaginationInterceptor();
   }


   /*
    * oracle数据库配置JdbcTypeForNull
    * 参考：https://gitee.com/baomidou/mybatisplus-boot-starter/issues/IHS8X
    不需要这样配置了，参考 yml:
    mybatis-plus:
      confuguration
        dbc-type-for-null: 'null' 
   @Bean
   public ConfigurationCustomizer configurationCustomizer(){
       return new MybatisPlusCustomizers();
   }

   class MybatisPlusCustomizers implements ConfigurationCustomizer {

       @Override
       public void customize(org.apache.ibatis.session.Configuration configuration) {
           configuration.setJdbcTypeForNull(JdbcType.NULL);
       }
   }
   */

   @Bean(name = "dataSource_one")
   //@Qualifier("primaryDataSource")
   @ConfigurationProperties(prefix="custom.datasource.ds1")
   public DataSource dataSource_one() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "dataSource_two")
   //@Qualifier("secondaryDataSource")
   @Primary
   @ConfigurationProperties(prefix="custom.datasource.ds2")
   public DataSource dataSource_two() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "dataSource")
   public DynamicDataSource dataSource(){
      DynamicDataSource dynamicDataSource = new DynamicDataSource();
      Map<Object, Object> hashMap = new HashMap<>();
      hashMap.put("dataSource_one",this.dataSource_one());
      hashMap.put("dataSource_two",this.dataSource_two());
      dynamicDataSource.setTargetDataSources(hashMap);
      dynamicDataSource.setDefaultTargetDataSource(this.dataSource_two());
      return dynamicDataSource;
   }

   @Bean("mybatisSqlSessionFactoryBean")
   @ConfigurationProperties(prefix = "mybatis-plus")
   @ConfigurationPropertiesBinding()
   public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(@Qualifier(value = "dataSource") DataSource dataSource) throws Exception {
      MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
      bean.setDataSource(dataSource);
      bean.setPlugins(new Interceptor[]{ //PerformanceInterceptor(),OptimisticLockerInterceptor()
              paginationInterceptor() //添加分页功能
      });
      ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
      Resource[] resources = resourceResolver.getResources("classpath:/mapper/*.xml");
      bean.setMapperLocations(resources);
      return bean;
   }

   @Bean("sqlSessionFactory")
   @Primary
   public SqlSessionFactory sqlSessionFactory() throws Exception {
      return mybatisSqlSessionFactoryBean(dataSource()).getObject();
   }
}